// Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MASTERSERVER_HEALTHCHECKER_H
#define MASTERSERVER_HEALTHCHECKER_H

#include <oatpp/web/server/HttpConnectionHandler.hpp>
#include <oatpp/network/Server.hpp>
#include <oatpp/network/tcp/server/ConnectionProvider.hpp>
#include "CommonMaster.h"
#include "Log.h"

class HealthChecker : public oatpp::web::server::HttpRequestHandler {
public:

    std::shared_ptr<OutgoingResponse> handle(const std::shared_ptr<IncomingRequest>& request) override {
        uint32_t idleWorkerCount = master::getIdleCount();
        if (idleWorkerCount > 0) {
            return ResponseFactory::createResponse(Status::CODE_200, "OK");
        } else {
            LOG_DEBUG_FMT("Idle worker count is %d", idleWorkerCount);
            return ResponseFactory::createResponse(Status::CODE_404, "Not Found");
        }
    }
};

#endif //MASTERSERVER_HEALTHCHECKER_H